#!/usr/bin/env python
import pygame
from math import floor
from Const import REGION_WIDTH, REGION_HEIGHT

class NonPhysics_object:
    bounce = 0   #bouce factor for coll_ obj
    friction = 1 #friction factor for coll_ obj
    x = 0
    y = 0
    w = 1
    h = 1
    health = 100
    destroyable = False
    region = []
    offset = (0, 0)
    hspeed = 0 #to avoid errors
    vspeed = 0 #  "
    def __init__(self):
        pass
    
    def init(self, level, x, y, image, bbox = None):
        #pygame.sprite.Sprite.__init__()
        self.x = x
        self.y = y
        self.level = level
        self.image = image
        if bbox != None:
            self.offset = (bbox[0], bbox[1])
            self.rect = (bbox[0] + x, bbox[1] + y, bbox[2], bbox[3])
        else:
            self.offset = (0, 0)
            self.rect = (x, y, image.get_width(), image.get_height())
        self.region = self.find_collision_region(level)
        font = pygame.font.SysFont("Arial", 28)
        self.text = font.render(str(self.region[0]), True, (255, 255, 255))
    
    def find_collision_region(self, level):
        ret = []
        rx = int(floor(self.rect[0] / REGION_WIDTH))
        ry = int(floor(self.rect[1] / REGION_HEIGHT))
        if (rx >= 0 and rx < level.hor_regions and
            ry >= 0 and ry < level.vert_regions):
            ret.append(level.region_indices[rx][ry])
        
        rx = int(floor((self.rect[0] + self.rect[2]) / REGION_WIDTH))
        ry = int(floor(self.rect[1] / REGION_HEIGHT))
        if (rx >= 0 and rx < level.hor_regions and
            ry >= 0 and ry < level.vert_regions):            
            if not ret.__contains__(level.region_indices[rx][ry]):
                ret.append(level.region_indices[rx][ry])
            
        rx = int(floor(self.rect[0] / REGION_WIDTH))
        ry = int(floor((self.rect[1] + self.rect[3]) / REGION_HEIGHT))
        if (rx >= 0 and rx < level.hor_regions and
            ry >= 0 and ry < level.vert_regions):
            if not ret.__contains__(level.region_indices[rx][ry]):
                ret.append(level.region_indices[rx][ry])
            
        rx = int(floor((self.rect[0] + self.rect[2]) / REGION_WIDTH))
        ry = int(floor((self.rect[1] + self.rect[3]) / REGION_HEIGHT))
        if (rx >= 0 and rx < level.hor_regions and
            ry >= 0 and ry < level.vert_regions):
            if not ret.__contains__(level.region_indices[rx][ry]):
                ret.append(level.region_indices[rx][ry])
        ret.sort()
        return ret
    
    def collision_other(self, other):
        pass
    def event_death(self):
        pass
    def hit(self, dammage, hit_pos):
        if self.destroyable:
            self.health -= dammage
            if self.health <= 0:
                self.event_death()
                self.level.remove(self, self.region)
    def override_back_draw(self, camera):
        pass
    
    def override_front_draw(self, camera):
        pass
    def draw(self, camera):
        self.override_back_draw(camera)
        camera.blit(self.image, (self.x, self.y))
        #camera.blit(self.text, (self.x, self.y))
        self.override_front_draw(camera)
